<template>
  <el-dialog
      v-model="ledgerEditWinDialogVisible"
      title="台账编辑"
      width="60%"
      @close="handleClose"
      draggable
      center
  >
    <template #default>
      <el-form :model="editLedger">
        <el-form-item label="设备编码">
          <el-input v-model="editLedger.ledgerEncode"/>
        </el-form-item>
        <el-form-item label="设备名称">
          <el-input v-model="editLedger.ledgerName"/>
        </el-form-item>
        <el-form-item label="品牌">
          <el-input v-model="editLedger.ledgerBrand"/>
        </el-form-item>
        <el-form-item label="设备分类">
          <el-select v-model="editLedger.ledgerType">
            <el-option label="家用" value="0"/>
            <el-option label="军用" value="1"/>
            <el-option label="企业" value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="规格型号">
          <el-input type="textarea" v-model="editLedger.ledgerModel"/>
        </el-form-item>
        <el-form-item label="所属车间">
          <el-select v-model="editLedger.ledgerWorkshop">
            <el-option label="家用车间" value="0"/>
            <el-option label="军用车间" value="1"/>
            <el-option label="企业车间" value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="editLedger.ledgerRemark"/>
        </el-form-item>

      </el-form>
    </template>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="editLedgerSubmit">保存</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import {ref, watch} from "vue";
import {ElMessage} from "element-plus";
import ledgerRequest from "@/api/ledger.js";

const props = defineProps(['ledgerId']);
const emits = defineEmits(['cancelEdit','editComplete' ])
let ledgerEditWinDialogVisible = ref(false)
let editLedger = ref({})


/**
 * 取消编辑的按钮
 */
function cancel() {
  emits("cancelEdit");
  ledgerEditWinDialogVisible.value = false;
}

/**
 * 关闭窗体前的回调函数
 */
function handleClose() {
  emits("cancelEdit");
}

function loadLedgerById() {
  ledgerRequest.selectById(props.ledgerId, res => {
    if (res.data) {
      editLedger.value = res.data;
      ledgerEditWinDialogVisible.value = true;
    }
  })
}

function editLedgerSubmit() {
  ledgerRequest.update(editLedger.value, res => {
    if (res.code == 1) {
      ElMessage({
        type: "success",
        message: "编辑成功"
      });
      ledgerEditWinDialogVisible.value = false;
      // 触发编辑完成的事件
      emits('editComplete');
    } else {
      ElMessage({
        type: "warning",
        message: "编辑失败"
      });
    }
  })
}

watch(() => props.ledgerId, () => {
  loadLedgerById();
});
</script>
<style scoped>

</style>